package cn.hzxs.zjn.mapper;

import cn.hzxs.zjn.pojo.Account;
import cn.hzxs.zjn.pojo.Folder;
import cn.hzxs.zjn.pojo.Note;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * @Author: Zjn ; Bring it on !!!
 * @Date: 2021/4/23 0023 7:45
 * Note:
 */

@Repository
public interface FoldersAndNotesMapper {

    @Select("select password from account where user_name=#{USERNAME}")
    String findPassWordByuUserName(String USERNAME);

    @Select("select user_name as userName,account_id as userId from account where user_name=#{USERNAME}")
    Map findInfoByUserName(String USERNAME);

    @Select("select note_id as noteId,title,detail,create_time as time from note where account_id=#{ACCOUNTID}")
    List<Map> getNotes(BigInteger ACCOUNTID);

    @Select("select folder_id as folderid,name as title,create_time as time from folder where " +
            "account_id=#{USERID}")
    List<Map> getFolders(BigInteger USERID);

    @Select("select note_id as noteId,title,detail,create_time as time from note where" +
            " account_id=#{userId} and folder_id=#{folderId}")
    List<Map> getFolderNotes(BigInteger folderId,BigInteger userId);

    @Insert("insert into note (title,detail,account_id,folder_id,create_time,update_time)" +
            " values(#{note.TITLE},#{note.DETAIL},#{note.ACCOUNTID},#{note.FOLDERID},current_date(),current_date())")
    @Options(useGeneratedKeys=true,keyProperty = "NOTEID",keyColumn = "note_id")
    int addNote(@Param("note") Note note);

    @Insert("insert into folder (account_id,name,create_time) " +
            "values (#{folder.ACCOUNTID},#{folder.NAME},current_date())")
    @Options(useGeneratedKeys=true,keyProperty = "FOLDERID",keyColumn = "folder_id")
    int addFolder(@Param("folder") Folder folder);

    @Select("select count(*) from note where account_id=#{ACCOUNTID}")
    String noteCount(BigInteger ACCOUNTID);

    @Select("select account_id from account where user_name=#{username}")
    BigInteger selectUserId(String username);
}
